#!bin/bash

if [ -n "$1" ] ; then
  data_date=$1
else
  data_date=`date -d '-1 day' +%F`
fi

DIM_ACTIVITY_PRODUCT_DETAIL_SQL="
INSERT OVERWRITE TABLE jtp_ds_warehouse.dim_activity_product_detail PARTITION (dt = '${data_date}')
SELECT
    apr.id AS relation_id,
    apr.activity_id,
    apr.product_id,
    apr.sku_id,
    apr.discount_amount AS activity_discount,
    apr.purchase_limit,
    apr.is_valid AS activity_product_valid,
    apr.add_time AS activity_add_time,
    ca.activity_name,
    ca.activity_level,
    ca.discount_type,
    ca.min_discount,
    ca.max_discount,
    ca.start_time AS activity_start_time,
    ca.end_time AS activity_end_time,
    ca.status AS activity_status,
    ca.create_time AS activity_create_time,
    ca.create_user,
    p.product_name,
    p.shop_id AS product_shop_id,
    p.original_price,
    p.status AS product_status,
    s.sku_name,
    s.sku_price,
    s.stock,
    s.spec_json,
    s.status AS sku_status
FROM jtp_ds_warehouse.ods_activity_product_relation apr

         LEFT JOIN jtp_ds_warehouse.ods_customer_service_discount_activity ca
                   ON apr.activity_id = ca.activity_id AND apr.dt = ca.dt
         LEFT JOIN jtp_ds_warehouse.ods_product_base_info p
                   ON apr.product_id = p.product_id AND apr.dt = p.dt
         LEFT JOIN jtp_ds_warehouse.ods_sku_info s
                   ON apr.sku_id = s.sku_id AND apr.dt = s.dt
WHERE apr.dt = '${data_date}'
  AND ca.dt = '${data_date}'
  AND p.dt = '${data_date}'
  AND s.dt = '${data_date}'
ORDER BY apr.add_time DESC;
"

/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -p 123456 -e "${DIM_ACTIVITY_PRODUCT_DETAIL_SQL}"